﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using EHR.Common;
using System.Collections.ObjectModel;

namespace CommunitySystem.PageView
{
    
    public partial class AddZKZWindow : ChildWindow
    {
        private List<InitKSSJ> KSSJLIst = new List<InitKSSJ>();
        private ZKZInfo info = new ZKZInfo() { CreateUser=Session.LoginUser.LoginName,CreateDate=DateTime.Now};
        private ZKZZYSXInfo zysxinfo = new ZKZZYSXInfo() { CreateUser = Session.LoginUser.LoginName, CreateDate = DateTime.Now };
       
        private JinShengServiceClient _client = new JinShengServiceClient();
        private string SelEmpIDs = "";
        private int exid { get; set; }
        public AddZKZWindow(ObservableCollection<ZKZEmpInfo> _selEmpZKZ)
        {
            InitializeComponent();
            exid = _selEmpZKZ.First().ExameID;
            foreach (var item in _selEmpZKZ)
            {
                SelEmpIDs += item.EmpID + ",";
            }
            _client.SaveZKZInfoCompleted += (sender, e) =>
            {
                if (e.Error == null)
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
                this.DialogResult = true;
            };

            _client.GetZKZZYSXInfoCompleted += (s, e) => {
                if (e.Result.Count > 0)
                {
                    NewZYSX = false;
                    zysxinfo = e.Result.First();
                }
                else
                {
                    NewZYSX = true;
                    zysxinfo = new ZKZZYSXInfo() { CreateUser = Session.LoginUser.LoginName, CreateDate = DateTime.Now };
                }
              
                this.spzysx.DataContext = zysxinfo;
            };
            _client.GetZKZZYSXInfoAsync(" 1=1 order by createDate desc");
           
            init();
            this.DataContext = info;
          

          
        }
        private bool vidata()
        {
            if (string.IsNullOrEmpty(info.KDMC))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入考点名称！");
                return false;
            }
            if (string.IsNullOrEmpty(info.KDDZ))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入考点地址！");
                return false;
            }
            if (string.IsNullOrEmpty(info.KCH))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入考场号！");
                return false;
            }
            if (string.IsNullOrEmpty(info.KSSJCOn))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入考试时间！");
                return false;
            }
            if (string.IsNullOrEmpty(info.ZKZZYSXInfoEntity.ZYSX))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入注意事项！");
                return false;
            }
            return true;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
      //KSSJLIst
            var sj = "";
            for (int i = 0; i < KSSJLIst.Count; i++)
            {
                sj += KSSJLIst[i].YY + "年" + KSSJLIst[i].MM + "月" + KSSJLIst[i].DD + "日"
                    +"  " + CaculateWeekDay(Convert.ToInt32(KSSJLIst[i].YY), Convert.ToInt32(KSSJLIst[i].MM), Convert.ToInt32(KSSJLIst[i].DD))
                    + ";" + KSSJLIst[i].EHH + ":" + KSSJLIst[i].ESS + "—" + KSSJLIst[i].DHH + ":" + KSSJLIst[i].DSS + ";" + KSSJLIst[i].KM + "/";
            }
            info.KSSJCOn = sj.TrimEnd('/');
            info.ZKZZYSXInfoEntity = zysxinfo;

            if (vidata())
                _client.SaveZKZInfoAsync(info, NewZYSX, SelEmpIDs.TrimEnd(','), exid);
        
        }

 

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        /// <summary>
        /// 考场时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            InitKSSJ kssj = new InitKSSJ();
            KSSJLIst.Add(kssj);
            spkssj.Children.Add(new KCSJControl(kssj));
        }
        InitKSSJ _InitKSSJ = null;
        private void init()
        {
            _InitKSSJ = new InitKSSJ();
            this.spinitkssj.DataContext = _InitKSSJ;
            KSSJLIst.Add(_InitKSSJ);
            List<string> year = new List<string>();
            for (int i = DateTime.Now.Year; i <= DateTime.Now.Year + 1; i++)
            {
                year.Add(i.ToString());
            }
            List<string> month = new List<string>();
            for (int i = 1; i <= 12; i++)
            {
                month.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            List<string> day = new List<string>();
            for (int i = 1; i <= 31; i++)
            {
                day.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            List<string> hh = new List<string>();
            for (int i = 1; i <= 24; i++)
            {
                hh.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            List<string> mm = new List<string>();
            for (int i = 0; i <= 59; i++)
            {
                mm.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }

            this.cbyear.ItemsSource = year;
            this.cbmonth.ItemsSource = month;
            //this.cbday.ItemsSource = day;
            this.cbdhh.ItemsSource = hh;
            this.cbehh.ItemsSource = hh;
            this.cbemm.ItemsSource = mm;
            this.cbdmm.ItemsSource = mm;
        }
        private void cbmonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var year = Convert.ToInt32(this.cbyear.SelectedValue.ToString());
            var month = Convert.ToInt32(this.cbmonth.SelectedValue.ToString());
            var dd = 31;
            if (year % 400 == 0 && month == 2)
                dd = 29;
            if (year % 400 != 0 && month == 2)
                dd = 28;
            List<string> day = new List<string>();
            for (int i = 1; i <= dd; i++)
            {
                day.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            this.cbday.ItemsSource = day;

            GetWeek();

        }
        /// <summary>
        /// 清空注意事项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool NewZYSX { get; set; }
        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            NewZYSX = true;
            zysxinfo = new ZKZZYSXInfo() { CreateUser = Session.LoginUser.LoginName, CreateDate = DateTime.Now };
            this.spzysx.DataContext = zysxinfo;
        }

        private void cbday_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetWeek();
          
        }

        private void GetWeek()
        {
            if (this.cbyear.SelectedValue == null) return;
            if (this.cbmonth.SelectedValue == null) return;
            if (this.cbday.SelectedValue == null) return;
            var year = Convert.ToInt32(this.cbyear.SelectedValue.ToString());
            var month = Convert.ToInt32(this.cbmonth.SelectedValue.ToString());
            var day = Convert.ToInt32(this.cbday.SelectedValue.ToString());

            //DateTime.Parse(textBox1.Text.Trim()).DayOfWeek.ToString();   
            this.tbweek.Text=CaculateWeekDay(year, month, day);
        }

        private string CaculateWeekDay(int y, int m, int d)
        {
            var ddd = y.ToString() + "-" + m.ToString() + "-" + d.ToString();
            return getCN(DateTime.Parse(ddd).DayOfWeek.ToString()); 
        }
        private string getCN(string en)
        {
            if (en.Equals("Monday"))
                return "星期一";
            else if (en.Equals("Tuesday"))
                return "星期二";
            else if (en.Equals("Wednesday"))
                return "星期三";
            else if (en.Equals("Thursday"))
                return "星期四";
            else if (en.Equals("Friday"))
                return "星期五";
            else if (en.Equals("Saturday"))
                return "星期六";
            else if (en.Equals("Sunday"))
                return "星期日";

            return "";

        }
        private void cbyear_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetWeek();
        } 

    }

    public partial class InitKSSJ {

        public string YY { get; set; }
        public string MM { get; set; }
        public string DD { get; set; }
        public string EHH { get; set; }
        public string ESS { get; set; }
        public string DHH { get; set; }
        public string DSS { get; set; }
        public string KM { get; set; }
    }
}

